﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

using PeachSystem.XBase.WX;
using PeachSystem.XBase.WX.Entites;


namespace PeachSystem.XCenter.WX
{
    /// <summary>
    /// x 的摘要说明
    /// </summary>
    public class x : IHttpHandler
    {
        #region 常量

        const string TOKEN = "05ad3a1c64d24ea7a2df89f41f863c4c";
        const string APPID = "wx25cb6777a3143395";
        const string APPSECRET = "fd80c94a98b3d18170903c2f5b78b77a";
        const string ENCODINGAESKEY = "iswnxWaacXhwiucoeLCrAqPPA5Br6yUniNtCyja5znT";

        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void ProcessRequest(HttpContext c)
        {
            string data = "";

            //第一次开发者认证为GET,  此后的数据传递为POST
            if (c.Request.HttpMethod.ToUpper() == "POST")
            {
                using (Stream stream = HttpContext.Current.Request.InputStream)
                {
                    Byte[] postBytes = new Byte[stream.Length];
                    stream.Read(postBytes, 0, (Int32)stream.Length);
                    data = Encoding.UTF8.GetString(postBytes);

                    if (!string.IsNullOrEmpty(data))
                    {
                        c.Response.ContentType = "text/html";

                        string msg_signature = c.Request["msg_signature"];
                        string timestamp = c.Request["timestamp"];
                        string nonce = c.Request["nonce"];

                        WXConfig config = new WXConfig(TOKEN, APPID, APPSECRET, ENCODINGAESKEY);
                        System.IO.File.AppendAllText(c.Server.MapPath("/log/wx.log"), "S:" + data + "\r\n");
                        string xml = new WXHelper(new WXConfig(TOKEN, APPID, APPSECRET, ENCODINGAESKEY)).GetMessage(msg_signature, timestamp, nonce, data);
                        System.IO.File.AppendAllText(c.Server.MapPath("/log/wx.log"), "R:" + xml + "\r\n");
                    }
                }
            }
            else
            {
                //第一次，进行开发认证，Get
                CheckSignature(c);
            }
        }

        /// <summary>
        /// 验证签名
        /// </summary>
        /// <param name="c">HttpContext</param>
        /// <returns>结果</returns>
        private bool CheckSignature(HttpContext c)
        {
            string signature = c.Request["signature"];
            string timestamp = c.Request["timestamp"];
            string nonce = c.Request["nonce"];
            string echostr = c.Request["echostr"];

            bool result = new WXHelper(new WXConfig(TOKEN, APPID, APPSECRET, ENCODINGAESKEY)).CheckSignature(signature, timestamp, nonce);

            if (result)
            {
                System.IO.File.AppendAllText(c.Server.MapPath("log/log.txt"), echostr + "\r\n");
                c.Response.Write(echostr);
            }
            else
            {
                System.IO.File.AppendAllText(c.Server.MapPath("log/log.txt"), "FALSE:" + echostr + "\r\n");
            }

            //
            return result;
        }
    }
}